public class HashBuck<K,V> {
    class Node<K,V>{
        K key;
        V val;
        Node<K,V> next;
        public Node(K key,V val){
            this.key = key;
            this.val = val;
        }
    }
    public Node<K,V>[] elem = (Node<K,V>[])new Node[10];
    public int usedSize;

    public static final double LOAD_FACTOR = 0.75;

    public void put(K key,V value) {
        int hash = key.hashCode() & Integer.MAX_VALUE;
        int index =  hash % elem.length;
        Node<K, V> cur = elem[index];
        while (cur != null) {
            Node<K, V> curNext = cur.next;
            if (cur.key.equals(key)) {
                cur.val = value;
                return;
            }
            cur = curNext;
        }
        Node<K, V> node = new Node<>(key, value);
        /*尾插
        if(cur == null){
            elem[index] = node;
            return;
        }
        cur.next = node;*/
        //头插
        node.next = elem[index];
        elem[index] = node;
        usedSize++;
        if (usedSize * 1.0 / elem.length >= 0.75){
            resize();
        }
    }

    private void resize() {
        Node<K,V>[] newArray = (Node<K,V>[])new Node[elem.length*2];
        for(int i = 0;i < elem.length;i++) {
            Node<K,V> cur = elem[i];
            while(cur != null) {
                int hash = cur.key.hashCode();
                int index = hash % newArray.length;
                cur.next = newArray[index];
                newArray[index] = cur;
                cur = cur.next;
            }
        }
        elem = newArray;
    }

    public V get(K key){
        for (int i = 0; i < elem.length; i++) {
            Node<K,V> cur = elem[i];
            while(cur != null) {
                if(cur.key.equals(key)) {
                    return cur.val;
                }
                cur = cur.next;
            }
        }
        return null;
    }
}
